﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace EtlComm
{
    /// <summary>
    /// 处理参数
    /// </summary>
    public class EtlEventArgs : EventArgs
    {
        public string Message { get; set; }
        /// <summary>
        /// 事件发生时间
        /// </summary>
        public DateTime EventTime { get; set; }
        /// <summary>
        /// 加油站ID
        /// </summary>
        public string StationID { set; get; }
        /// <summary>
        /// 任务ID
        /// </summary>
        public string TaskID { set; get; }
        /// <summary>
        /// 调度进程状态
        /// </summary>
        public DispatchProcess DProcess { set; get; }
        /// <summary>
        /// 抽取的记录数
        /// </summary>
        public int ExtractCount { get; set; }
        /// <summary>
        /// 成功加载记录数
        /// </summary>
        public int LoadCount { get; set; }
    }
    /// <summary>
    /// 日志信息事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public delegate void EtlProcess(object sender, EtlEventArgs e);
    /// <summary>
    /// 执行引擎
    /// </summary>
    public interface IEtlInterface
    {
        IExtracter DataExtract { get; set; }
        ILoader DataLoad { get; set; }
        ILog log{set;get;}
        TaskObject Task { get; set; }
        /// <summary>
        /// 加载记录数
        /// </summary>
        int LoadCount { get; }
        /// <summary>
        /// 抽取记录数
        /// </summary>
        int ExtrCount { get; }
        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="StationID">加油站ID</param>
        /// <returns>执行结果</returns>
        bool Execute(string StationID);
    }
    /// <summary>
    /// 数据抽取(源数据)
    /// </summary>
    public interface IExtracter
    {
        /// <summary>
        /// 连接串
        /// </summary>
        string ip { set; get; }
        /// <summary>
        /// 任务ID
        /// </summary>
        int TaskID { set; get; }
        /// <summary>
        /// 加油站ID
        /// </summary>
        string StationID { get; set; }
        /// <summary>
        /// 任务主键
        /// </summary>
        string PrimaryKey { get; set; }
        /// <summary>
        /// 执行SQL取数据
        /// </summary>
        /// <param name="strSQL">SQL语句</param>
        /// <returns>数据表</returns>
        DataTable GetResult(string strSQL);
        /// <summary>
        /// 取数据库的时间
        /// </summary>
        /// <returns></returns>
        DateTime GetDBTime();
        /// <summary>
        /// 取MAX ID
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="pk">主健</param>
        /// <returns>ID</returns>
        /// 如果表名为DB_MAXID则取抽取记录的最大ID
        /// 其它则取实际的表的最大ID
        int GetMaxID(string table,string pk);
    }
    /// <summary>
    /// 数据加载(目标数据)
    /// </summary>
    public interface ILoader
    {
        string ip{set;get;}
        SQLAction Action{set;get;}
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="result">数据表</param>
        /// <returns>成功返回true</returns>
        bool SetResult(DataTable result);
    }
    /// <summary>
    /// 日志接口
    /// </summary>
    public interface ILog
    {
        string ConnectString { get; set; }
        bool WriteLog(string loginfo);
        bool WriteLog(string StationID, string TaskID, string loginfo);
        bool WriteLog(string StationID, string TaskID, string loginfo,LogType logtype);
        event EtlProcess OnTaskProcess;
    }
    /// <summary>
    /// 数据工厂
    /// </summary>
    public abstract class DataFactory
    {
        /// <summary>
        /// 创建数据抽取器
        /// </summary>
        /// <returns></returns>
        public virtual IExtracter CreateExtrater()
        {
            return null;
        }
        /// <summary>
        /// 创建数据加载器
        /// </summary>
        /// <returns></returns>
        public virtual ILoader CreateLoader()
        {
            return null;
        }
        /// <summary>
        /// 创建映射器
        /// </summary>
        /// <returns></returns>
        public virtual IMap CreateMap()
        {
            return null;
        }
    }
    /// <summary>
    /// 数据关系映射
    /// </summary>
    public interface IMap
    {
        /// <summary>
        /// 根据任务ID取列映射
        /// </summary>
        /// <param name="TaskID">任务ID</param>
        /// <returns>列映射</returns>
        IDictionary<string,TableKey> Map(int TaskID);
        /// <summary>
        /// 根据任务ID取映射表名
        /// </summary>
        /// <param name="TaskID">任务ID</param>
        /// <returns>映射表名</returns>
        string GetTableNameByTaskID(int TaskID);
    }
}
